package com.stedu.mall.userservice.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.User;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.UserService;
import com.stedu.mall.userservice.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@DubboService
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> selectAllByCondition(User condition, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //查询
        List<User> userList = userMapper.selectAllByCondition(condition);
        //创建分页信息
        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        return userPageInfo;
    }

    @Override
    public User login(String username, String password) throws SteduException {
        //根据用户名查询，判断用户是否存在
        User user = userMapper.selectByUserName(username);
        if(user == null){
            throw new SteduException("该用户名不存在，无法登陆");
        }
        //判断用户是否禁用或者注销
        if(user.getStatus().equals(2)||user.getStatus().equals(3)){
            throw new SteduException("该用户已被禁用或者注销，无法登陆");
        }

        //对输入的密码进行加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(password + user.getSalt()));
        if(!md5Pwd.equals(user.getPassword())){
            throw new SteduException("密码错误，登陆失败");
        }
        //和数据库的密码对比
        return user;
    }

    @Override
    public boolean reg(User user) throws SteduException {
        //判断该用户名在系统中是否存在
        if(userMapper.selectByUserName(user.getUsername()) != null){
            throw new SteduException("该用户名已存在，注册失败");
        }
        //生成盐
        String salt = RandomUtil.randomString(8);
        user.setSalt(salt);

        //对密码进行加密
        user.setPassword(SecureUtil.md5(SecureUtil.md5(user.getPassword() + salt)));

        //添加用户
        return userMapper.insert(user) == 1;


    }

    @Override
    public User selectById(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public boolean update(User user) throws SteduException {
        if (userMapper.selectByUserName(user.getUsername()) != null
                && userMapper.selectByUserName(user.getUsername()).getUsername().equals(user.getUsername())
                && userMapper.selectByUserName(user.getUsername()).getId() != user.getId()) {
            throw new SteduException("该用户名已存在，修改失败");
        }
        if (user.getMoney() != null) {
            BigDecimal money = userMapper.selectById(user.getId()).getMoney();
            BigDecimal add = money.add(user.getMoney());
            user.setMoney(add);
        }
        return userMapper.update(user) == 1;
    }
    //修改密码
    @Override
    public boolean updatePassword(String oldPassword, String newPassword, String confirmPassword, Integer userId) throws SteduException {
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new SteduException("该用户不存在，请重新登录");
        }
        if (!newPassword.equals(confirmPassword)) {
            throw new SteduException("两次输入的密码不一致，请重新输入");
        }
        if (!user.getPassword().equals(SecureUtil.md5(SecureUtil.md5(oldPassword + user.getSalt())))) {
            throw new SteduException("原密码错误，请重新输入");
        }
        user.setPassword(SecureUtil.md5(SecureUtil.md5(newPassword + user.getSalt())));
        return userMapper.update(user) == 1;
    }
    //修改支付密码
    @Override
    public boolean updatePayPassword(String loginPassword, String newPayPassword, String confirmPayPassword, Integer userId) throws SteduException {
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new SteduException("该用户不存在，请重新登录");
        }
        if (!newPayPassword.equals(confirmPayPassword)) {
            throw new SteduException("两次输入的密码不一致，请重新输入");
        }
        if (!user.getPassword().equals(SecureUtil.md5(SecureUtil.md5(loginPassword + user.getSalt())))) {
            throw new SteduException("原密码错误，请重新输入");
        }
        user.setPayPassword(SecureUtil.md5(SecureUtil.md5(newPayPassword + user.getSalt())));
        return userMapper.update(user) == 1;
    }
    //支付
    @Override
    public boolean pay(User user) throws SteduException {
        return userMapper.update(user) == 1;
    }
}
